package com.scs.application.modules.fsd.controller;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.PinYinUtils;
import com.scs.application.modules.fsd.entity.FsdStation;
import com.scs.application.modules.fsd.service.StationService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @Description: 站点设置
 */
@RestController
@RequestMapping("/back_front/stations")
public class StationController extends CrudController<StationService, FsdStation> {
    @GetMapping(value = "/getService")
    public RestResult getPage(@RequestParam("keyword") String keyword, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        Map<String, Object> stationPage = service.getStation(keyword,currentPage,pageSize);
        return RestResult.ok("获取站点成功!",stationPage);
    }

    @GetMapping(value = "/getAllService")
    public RestResult getAll(){
        return findAll(true);
    }

    @PostMapping(value = "/updateClientService")
    public RestResult updateClient(@RequestBody Map<String, String> map){
        if(service.updateClient(map.get("stationCode")))
            return RestResult.ok("更新成功！");
        return RestResult.error("更新失败！");
    }

    @GetMapping(value = "/getBindStationCodeService")
    public RestResult getBindStationCode(){
        return RestResult.ok("获取站点可绑定的SPD库位编码！",service.getBindStationCode());
    }

    /**
     * scsDataviewDown数据视图
     * @return
     */
    @GetMapping(value = "/getBindStationCodeView")
    public RestResult getBindStationCodeView(){
        return RestResult.ok("获取站点可绑定的SPD库位编码！",service.getBindStationCodeView());
    }

    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public RestResult saveItem(@Valid @RequestBody FsdStation entity) {
        entity.setStationNamePy(PinYinUtils.getAlpha(entity.getStationName()));

        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        Map result = service.savehumiture(entity);
        if (result.get("success").equals("false")) return RestResult.error("数据校验失败!"+result.get("msg"));

        return RestResult.ok("数据保存成功",  entity);
    }
}
